package de.global42.clubapp.ejbs;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.Asynchronous;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

import de.global42.clubapp.model.Client;
import de.global42.clubapp.model.ClientCoupon;
import de.global42.clubapp.model.ClientMission;
import de.global42.clubapp.model.Coupon;

@Stateless
public class ClientCouponHandlerBean implements ClientCouponHandler {

	private EntityAccessor entityAccessor;
	
	private EntityManager entityManager;
	
	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public List<Coupon> getClientCoupons(Long clientId) {
		List<Coupon> coupons = new ArrayList<Coupon>();		
		Client client;
		try {
			client = entityAccessor.getEntity(Client.class, "identifier", clientId.toString());
			for (ClientMission clientMission : client.getClientMissions()) {
				if(clientMission.getClientCoupon().getDevaluatedOn() == null)
					coupons.add(clientMission.getClientCoupon().getCoupon());
			}
		} catch (NoResultException e) {
			// TODO Auto-generated catch block
		} catch (Exception e) {
			// TODO Auto-generated catch block
		}
		return coupons;
	}

	//TODO: Interceptor for flushing!
	@Override
	public void checkForCoupons(Long clientId) {
		
		try {
			Client client = entityAccessor.getEntity(Client.class, "identifier", clientId.toString());
			for (ClientMission clientMission : client.getClientMissions()) {
				
				if (clientMission.isComplete() && clientMission.getClientCoupon() == null) {
					
					ClientCoupon newClientCoupon = new ClientCoupon();
					
					newClientCoupon.setClientMission(clientMission);
					newClientCoupon.setCoupon(clientMission.getMission().getCoupon());
					newClientCoupon.setAssignedOn(new Date());
					
					entityManager.persist(newClientCoupon);
				}
			}
			entityManager.flush();
		} catch (NoResultException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

	@Override
	@Asynchronous
	public void checkForCouponsAsync(Long clientId) {
		checkForCoupons(clientId);
	}
	
	@Override
	public String devaluateCoupon(Long clientId, Long couponId) {
		String validationPhrase = new String();
		try {
			ClientCoupon clientCoupon = entityAccessor.getEntity(ClientCoupon.class, "id", couponId.toString());
			if (clientCoupon.isStillValid()) {
				clientCoupon.devaluate();
				
				Long helperLong = couponId+clientId;
				validationPhrase =String.valueOf(helperLong.hashCode());
			}
		} catch (NoResultException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return validationPhrase;
	}

	@EJB
	public void setEntityAccessor(EntityAccessor entityAccessor) {
		this.entityAccessor = entityAccessor;
	}

	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
}
